<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/15
 * Time: 16:55
 */

namespace App\Console\Service\IPProxyPool\Inc;

use Symfony\Component\DomCrawler\Crawler;

class Rule
{
    /**
     * @var array
     */
    public static $parserList = [

        [
            'urls' => ['http://ip.seofangfa.com/proxy/%s.html', [1, 1]],
            'code' => 'UTF-8',
            'type' => 'xpath',
            'pattern' => [".//html/body/div[2]/div[1]/table/tbody[1]", "tr[position()>1]"],
            'position' => ['ip' => '//td[0]', 'port' => '//td[1]', 'type' => '', 'protocol' => '']
        ],

        // www.xicidaili.com
//        [
//            'urls' => ['http://www.xicidaili.com/nn/%s', [1, 10]],
//            'code' => 'UTF-8',
//            'type' => 'xpath',
//            'pattern' => [".//*[@id=\"ip_list\"]", "tr[position()>1]"],
//            'position' => ['ip' => '//td[2]', 'port' => '//td[3]', 'type' => '//td[4]', 'protocol' => '//td[5]']
//        ],
//        [
//            'urls' => ['http://www.xicidaili.com/nt/%s', [1, 10]],
//            'code' => 'UTF-8',
//            'type' => 'xpath',
//            'pattern' => [".//*[@id=\"ip_list\"]", "tr[position()>1]"],
//            'position' => ['ip' => '//td[2]', 'port' => '//td[3]', 'type' => '//td[4]', 'protocol' => '//td[5]']
//        ],
//        [
//            'urls' => ['http://www.xicidaili.com/wn/%s', [1, 10]],
//            'code' => 'UTF-8',
//            'type' => 'xpath',
//            'pattern' => [".//*[@id=\"ip_list\"]", "tr[position()>1]"],
//            'position' => ['ip' => '//td[2]', 'port' => '//td[3]', 'type' => '//td[4]', 'protocol' => '//td[5]']
//        ],
//        [
//            'urls' => ['http://www.xicidaili.com/wt/%s', [1, 10]],
//            'code' => 'UTF-8',
//            'type' => 'xpath',
//            'pattern' => [".//*[@id=\"ip_list\"]", "tr[position()>1]"],
//            'position' => ['ip' => '//td[2]', 'port' => '//td[3]', 'type' => '//td[4]', 'protocol' => '//td[5]']
//        ],
        // www.66ip.cn
//        [
//            'urls' => ['http://www.66ip.cn/%s.html', [2, 12]],
//            'code' => 'gb2312',
//            'type' => 'xpath',
//            'pattern' => [".//*[@id='main']/div/div[1]/table", "tr[position()>1]"],
//            'position' => ['ip' => '//td[1]', 'port' => '//td[2]', 'type' => '//td[4]', 'protocol' => '']
//        ],
//        [
//            'urls' => ['http://www.66ip.cn/areaindex_%s/%s.html', [1, 35], [1, 10]],
//            'code' => 'gb2312',
//            'type' => 'xpath',
//            'pattern' => [".//*[@id='footer']/div/table", "tr[position()>1]"],
//            'position' => ['ip' => '//td[1]', 'port' => '//td[2]', 'type' => '//td[4]', 'protocol' => '']
//        ]
    ];

    /**
     * 获取采集任务列表
     * @return array
     */
    public static function getUrlsList()
    {
        $proxyList = [];
        $parserList = self::$parserList;
        foreach ($parserList as $key => $item) {
            $urlList = self::setUrlsPage($item);
            $proxyList = array_merge($proxyList, $urlList);
        }
        return $proxyList;
    }

    /**
     * 根据url规则生成url列表
     * @param array $urls
     * @return array
     */
    private static function setUrlsPage(array $item)
    {
        $urls = $item['urls'];
        $urlList = [];
        $itemNum = count($urls);
        if ($itemNum == 2) {
            for ($i = $urls[1][0]; $i <= $urls[1][1]; $i++) {
                $item["url"] = sprintf($urls[0], $i);
                $urlList[] = $item;
            }
        } elseif ($itemNum == 3) {
            for ($i = $urls[1][0]; $i <= $urls[1][1]; $i++) {
                for ($j = $urls[2][0]; $j <= $urls[2][1]; $j++) {
                    $item["url"] = sprintf($urls[0], $i, $j);
                    $urlList[] = $item;
                }
            }
        }
        return $urlList;
    }

    /**
     * 采集规则
     * Crawler 参考地址 : http://symfony.com/doc/current/components/dom_crawler.html
     * @param string $body
     * @param string $code
     * @param array $pattern
     * @param array $position
     * @return array
     */
    public static function getProxy(string $body, string $code = "UTF-8", array $pattern, array $position)
    {
        $crawler = new Crawler();
        $crawler->addHtmlContent($body, $code);
        $proxyList = $crawler->filterXPath(".//html/body/div[2]/tbody[1]/tr[position()>1]")->html();
        dd($proxyList);



        $proxyList = $crawler->filterXPath($pattern[0])->evaluate($pattern[1])->each(function (Crawler $node, $i) use ($position) {
            $ProxyItem = [];
            foreach ($position as $key => $pos) {
                $ProxyItem[$key] = $pos ? $node->filterXPath($pos)->html() : "";
            }
            return $ProxyItem;
        });
        return $proxyList;
    }

}